﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class RoleService
    {
        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Role GetRole(long id)
        {
            Role role = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    role = new Role();
                    role.Id = id;
                    role = new RoleDAO().GetRole(conn, role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return role;
        }

        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable GetRoleTable(int pageindex, int pagesize)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Role role = new Role();
                    role.Pageindex = pageindex;
                    role.Pagesize = pagesize;
                    dt = new RoleDAO().GetRoleTable(conn, role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询角色记录数
        /// </summary>
        /// <returns></returns>
        public long GetRoleRecord()
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Role role = new Role();
                    record = new RoleDAO().GetRoleRecord(conn, role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 是否存在相同角色名称
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool ExistSameRolename(long roleid, String rolename)
        {
            bool b = false;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Role role = new Role();
                    role.Id = roleid;
                    role.Name = rolename;
                    DataTable dt = new RoleDAO().GetRoleTable(conn, role);
                    foreach (DataRow row in dt.Rows)
                    {
                        long temp_id = (long)row["id"];
                        if (roleid != temp_id)
                        {
                            b = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return b;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public long InsertRole(Role role)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new RoleDAO().InsertRole(conn, role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public int UpdateRole(Role role)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new RoleDAO().UpdateRole(conn, role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteRole(String ids)
        {
            int count = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        trans = conn.BeginTransaction();
                        String[] arr = ids.Split(new Char[] { ',' });
                        RoleDAO roleDAO = new RoleDAO();
                        Role role = new Role();
                        RolePurviewService rolePurviewService = new RolePurviewService();
                        foreach (String lopid in arr)
                        {
                            role.Id = Int64.Parse(lopid);
                            int c = roleDAO.DeleteRole(conn, role);
                            if (c > 0)
                            {
                                int flag = rolePurviewService.DeleteRolePurview(conn, Int64.Parse(lopid));
                                trans.Commit();
                                count += c;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return count;
        }
    }
}
